package yuzhou;

import java.util.Arrays;

public class LPMODULEENTRY32 {
    public Integer size;
    public Integer mid;
    public Integer pid;
    public Integer gusage;
    public Integer pusage;
    public Integer base;
    public Integer bsize;
    public Integer hmodule;
    public String szname;
    public String szpath;

    @Override
    public String toString() {
        return "LPMODULEENTRY32{" +
                "size=" + size +
                ", mid=" + mid +
                ", pid=" + pid +
                ", gusage=" + gusage +
                ", pusage=" + pusage +
                ", base=" + base +
                ", bsize=" + bsize +
                ", hmodule=" + hmodule +
                ", szname='" + szname + '\'' +
                ", szpath='" + szpath + '\'' +
                '}';
    }

    //解析PMODULEENTRY32结构
    public static LPMODULEENTRY32 getInfo(byte[] PMODULEENTRY32){
        LPMODULEENTRY32 lp = new LPMODULEENTRY32();

        for (int i=0,n=1;i<32;i+=4){
            byte data[] = {PMODULEENTRY32[i+3],PMODULEENTRY32[i+2],PMODULEENTRY32[i+1],PMODULEENTRY32[i]};
            int arg = BytesUtil.bytes2Int(data);

            if(arg!=0){
                switch (n){
                    case 1:lp.size = arg;break;
                    case 2:lp.mid = arg;break;
                    case 3:lp.pid = arg;break;
                    case 4:lp.gusage = arg;break;
                    case 5:lp.pusage = arg;break;
                    case 6:lp.base = arg;break;
                    case 7:lp.bsize = arg;break;
                    case 8:lp.hmodule = arg;
                }
                n++;
            }
        }
        byte name[] = new byte[0];

        for (int i=32,n=0;PMODULEENTRY32[i]!=0;i++,n++) {
            name =Arrays.copyOf(name,name.length+1);
            name[n] = PMODULEENTRY32[i];
        }
        lp.szname = new String(name);

        byte path[] = new byte[0];
        for (int i=288,n=0;PMODULEENTRY32[i]!=0;i++,n++) {
            path =Arrays.copyOf(path,path.length+1);
            path[n] = PMODULEENTRY32[i];
        }
        lp.szpath = new String(path);

        return lp;
    }
    //生成PMODULEENTRY32结构
    public static byte[] getLPMODULEENTRY32(){
        byte[] info = new byte[544];
        //size = 1024
        info[1] = 4;
        return info;
    }
}
